package com.solocredit.finance.ui.loan.fm

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import com.solocredit.finance.R
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.databinding.FmMullaLoanPaymentSuccessBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.ProFm
import com.solocredit.finance.ui.loan.act.MullaLoanPaymentStatusAct
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/20 17:04
 * @Annotation：支付成功
 */
class MullaLoanPaymentSuccessFm(): ProFm() {
    private var _binding: FmMullaLoanPaymentSuccessBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )
    var act: MullaLoanPaymentStatusAct? = null
    private var orderData: JSONObject? = null

    constructor(act: MullaLoanPaymentStatusAct,data: JSONObject): this(){
        this.act = act
        this.orderData = data
    }

    override fun getMullaRootView(inflater: LayoutInflater): View {
        _binding = FmMullaLoanPaymentSuccessBinding.inflate(inflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTitleBarColor(Color.parseColor("#F8F8F8"))
        setMullaBackBtnRes(R.mipmap.mulla_back_black)
        setMullaTextTitle(getString(R.string.loan_payment_status_title))
        setMullaRightBtnRes(R.mipmap.mulla_kefu_icon)
    }

    override fun initMullaListener() {
        //客服
        rootViewBinding.ivFun.setOnClickListener {
            act?.let {
                showMullaKefuDialog("PaymentSuccess Page ${act?.loadProductName()}",it.loadProductName(),it.loadProductToken())
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_CUSTOMER,"PaymentSuccess Page ${it.loadProductName()}")
            }
        }

        _binding?.btnApply?.setOnClickListener {
            orderData?.let {
                HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_LOAN_PAYMENT_SUCCESS_APPLY_AGAIN,"TransactionId：${it.optString("transactionId")}")
            }
            act?.showMullaProductsListAction()
        }
    }

    override fun initMullaData() {
        orderData?.let {
            onRefreshData(it)
        }

        //牌照信息
        val licenseInfo = MullaCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.visibility = View.GONE
        } else {
            _binding?.licenseLayout?.removeAllViews()
            _binding?.licenseLayout?.addView(licenseView())
            _binding?.licenseLayout?.visibility = View.VISIBLE
        }
    }

    /**
     * 刷新数据
     */
    private fun onRefreshData(data: JSONObject){
        orderData = data
        _binding?.textAmount?.text = data.optString("realRepayAmount","")
        _binding?.textName?.text = data.optString("product","")
        _binding?.textThirdOrderId?.text = data.optString("transactionId","")
        _binding?.textCreateTime?.text = data.optString("createTime","")
        _binding?.textSuccessTime?.text = data.optString("realRepayTime","")
    }

    override fun onStart() {
        super.onStart()
        act?.let {
            HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_LOAN_REPAYMENT_SUCCESS_START,it.loadProductName())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        act?.let {
            HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_LOAN_REPAYMENT_SUCCESS_END,it.loadProductName())
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}